#include <json.h>
#include <stdio.h>

void json_parse(json_object * jobj) {
    enum json_type type;
    json_object_object_foreach(jobj, key, val) {
        //type = json_object_get_type(val);
        //switch (type) {
        //    case json_type_string: printf("type: json_type_string, ");
        
                printf("%s =>%s\n", key, json_object_get_string(val));
        
        if (key == 'result'){
            json_object * jobj = json_tokener_parse(json_object_get_string(val));
            json_parse(jobj);
        }
        
       //         break;
        //}
    }
}

int main()
{
    
    char *  string  = "{\"jsonrpc\":\"2.0\",\"id\":0,\"result\":[0,{\"device_id\":\"INyRyS5Y6166\",\"device_name\":\"Ren\",\"device_sn\":\"ACPZ0FC206166\",\"system_name\":\"挖矿固件\",\"system_version\":\"V1.2.135\",\"hardware_model\":\"WS1508\",\"mac_address\":\"B0:D5:9D:62:86:EF\",\"lan_ip\":\"192.168.1.143\",\"account_id\":\"469956463\",\"account_name\":\"469956463\",\"product_id\":321,\"disk_quota\":\"10995116277760\",\"schedule_hours\":\"[ { \\\"from\\\": 0, \\\"to\\\": 24, \\\"type\\\": \\\"unlimit\\\", \\\"params\\\": { } } ]\"}]}";
    

    printf ("JSON string: %s\n", string);
    json_object * jobj = json_tokener_parse(string);
    json_parse(jobj);
    
    struct json_object *result_object = NULL;
    struct json_object *result_object1 = NULL;
    struct json_object *result_object2 = NULL;
    
    result_object =  json_object_object_get(jobj, "result");
    printf("-----------result_object method ---------------------------\n");
    printf("%s\n", json_object_to_json_string(result_object));
    printf("-----------result_object method---------------------------\n");
    
    
    result_object =  json_object_array_get_idx(result_object, 1);
    result_object1 =  json_object_object_get(result_object, "device_id");
    printf("-----------device_id method ---------------------------\n");
    printf("%s\n", json_object_to_json_string(result_object1));
    printf("-----------device_id method---------------------------\n");

    result_object2 =  json_object_object_get(result_object, "device_name");
    printf("-----------device_name method ---------------------------\n");
    printf("%s\n", json_object_to_json_string(result_object2));
    printf("-----------device_name method---------------------------\n");

    /*
    char passsend[1000];
    sprintf(passsend, "{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":\"call\",\"params\":[\"\",\"netcfg\",\"set_device_discovery\",{\"device_id\":\"INyRyS5Y6166\",\"device_name\":\"jj\",\"device_password\":\"u6ZPLocu\"}]}");
    
    printf("%s\n\n",passsend);
    */
    char * device_id = "INyRyS5Y6166";
    char * device_name = "kuangji";
    char * device_password ="000000";


}
